﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;

namespace TS_XY_Train_Repaire.Locomotive.TrainRepair
{
    public partial class Locomotive_Repair_Electric_Work_Main_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadWorkType = "2";
        public Locomotive_Repair_Electric_Work_Main_ManaForm()
        {
            InitializeComponent();
        }

        private void Locomotive_Repair_Electric_Work_Main_ManaForm_Load(object sender, EventArgs e)
        {
            #region//--初始化色块规则--//
            DevExpress.XtraEditors.FormatConditionRuleValue[,] fcr_Main = new FormatConditionRuleValue[63, 9];
            for (int x = 1; x < 63; x++)
            {
                for (int y = 1; y < 9; y++)
                {
                    fcr_Main[x, y] = new DevExpress.XtraEditors.FormatConditionRuleValue();
                    fcr_Main[x, y].Appearance.Options.UseBackColor = true;
                    fcr_Main[x, y].Condition = DevExpress.XtraEditors.FormatCondition.Equal;
                    switch (y)
                    {
                        case 1:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.White;
                            fcr_Main[x, y].Value1 = "White";
                            break;
                        case 2:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Gray;
                            fcr_Main[x, y].Value1 = "Gray";
                            break;
                        case 3:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Red;
                            fcr_Main[x, y].Value1 = "Red";
                            break;
                        case 4:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Yellow;
                            fcr_Main[x, y].Value1 = "Yellow";
                            break;
                        case 5:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.LightBlue;
                            fcr_Main[x, y].Value1 = "LightBlue";
                            break;
                        case 6:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Magenta;
                            fcr_Main[x, y].Value1 = "Magenta";
                            break;
                        case 7:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Cyan;
                            fcr_Main[x, y].Value1 = "Cyan";
                            break;
                        case 8:
                            fcr_Main[x, y].Appearance.BackColor = System.Drawing.Color.Green;
                            fcr_Main[x, y].Value1 = "Green";
                            break;
                    }
                }
            }
            DevExpress.XtraGrid.GridFormatRule _gfr = new DevExpress.XtraGrid.GridFormatRule();
            for (int i = 1; i < 32; i++)
            {
                for (int n = 1; n < 9; n++)
                {
                    _gfr = new DevExpress.XtraGrid.GridFormatRule();
                    _gfr.Column = this.gv_Main.Columns["f_c" + i.ToString().PadLeft(2, '0')];
                    _gfr.ColumnApplyTo = this.gv_Main.Columns["f_d" + i.ToString().PadLeft(2, '0')];
                    _gfr.Name = "Format_" + i.ToString().PadLeft(2, '0') + "_" + n.ToString();
                    _gfr.Rule = fcr_Main[i, n];
                    gv_Main.FormatRules.Add(_gfr);
                }
            }
            for (int i = 32; i < 63; i++)
            {
                for (int n = 1; n < 9; n++)
                {
                    _gfr = new DevExpress.XtraGrid.GridFormatRule();
                    _gfr.Column = this.gv_Main_P.Columns["f_c" + (i-31).ToString().PadLeft(2, '0')];
                    _gfr.ColumnApplyTo = this.gv_Main_P.Columns["f_d" + (i - 31).ToString().PadLeft(2, '0')];
                    _gfr.Name = "Format_p" + i.ToString().PadLeft(2, '0') + "_" + n.ToString();
                    _gfr.Rule = fcr_Main[i, n];
                    gv_Main_P.FormatRules.Add(_gfr);
                }
            }
            //gc_Main.Refresh();
            #endregion

            _ReloadData();
        }
        private void _ReloadData()
        {
            try
            {
                InitTrainInList();
                //InitTrainInList_W();
                _LoadChartTrainIn();
                _LoadChartTrainW();
                _LoadChartTrainWorkPlace();
                _LoadChartDepWorkPlace();
                _LoadTrainInSched();
                _LoadTrainPlaneSched();
            }
            catch
            {

            }
        }
        private void Locomotive_Repair_Electric_Work_Main_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
        #region//--检修数据一览--//
        //刷新在修机车统计图
        private void _LoadChartTrainIn()
        {
            string strSQL = "select count(*) as f_count,f_ttname,(select f_name from TS_S_T_P_Train_RP where f_id = TS_TR_BU_Work_Main.f_rpid)as f_rpname from TS_TR_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) and f_type=" + _LoadWorkType + " group by f_ttname,f_rpid";
            cc_Train.Series[0].Points.Clear();
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            //MessageBox.Show(strSQL_TrainCount_Base + strSQL_WorkPlace_BMWID + _strQuery + ") and f_del=0 group by f_ttname,f_rpid");
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                cc_Train.Series[0].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_ttname"].ToString() + " " + read["f_rpname"].ToString(), new object[] { ((object)((int)read["f_count"])) }));
            }
            read.Close();
            conn.Close();
            conn.Dispose();
        }
        //刷新计划修机车统计图
        private void _LoadChartTrainW()
        {
            string strSQL = "select count(*) as f_count,f_ttname,(select f_name from TS_S_T_P_Train_RP where f_id = TS_TR_BU_Work_Main.f_rpid)as f_rpname from TS_TR_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state in (-1)) and f_type=" + _LoadWorkType + " group by f_ttname,f_rpid";
            cc_Train_W.Series[0].Points.Clear();
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            //MessageBox.Show(strSQL_TrainCount_Base + strSQL_WorkPlace_BMWID + _strQuery + ") and f_del=0 group by f_ttname,f_rpid");
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                cc_Train_W.Series[0].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_ttname"].ToString() + " " + read["f_rpname"].ToString(), new object[] { ((object)((int)read["f_count"])) }));
            }
            read.Close();
            conn.Close();
            conn.Dispose();
        }
        //刷新在修机车任务统计图
        private void _LoadChartTrainWorkPlace()
        {
            string strSQL = "select f_ttname,(select f_name from TS_S_T_P_Train_RP where f_id = TS_TR_BU_Work_Main.f_rpid)as f_rpname,f_name,(select count(*) from TS_TR_BU_Work_Main_Group_WorkPlace where f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) and f_del=0 and f_bmwid=TS_TR_BU_Work_Main.f_id) as f_scount,(select count(*) from TS_TR_BU_Work_Main_Group_WorkPlace where f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state in (-1)) and f_del=0 and f_bmwid=TS_TR_BU_Work_Main.f_id) as f_pcount,(select count(*) from TS_TR_BU_Work_Main_Group_WorkPlace where f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state in (9)) and f_del=0 and f_bmwid=TS_TR_BU_Work_Main.f_id) as f_ecount from TS_TR_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) and f_type=" + _LoadWorkType + " order by f_ttname,f_name asc";
            cc_Train_WorkPlace.Series[0].Points.Clear();
            cc_Train_WorkPlace.Series[1].Points.Clear();
            cc_Train_WorkPlace.Series[2].Points.Clear();
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            //MessageBox.Show(strSQL_TrainCount_Base + strSQL_WorkPlace_BMWID + _strQuery + ") and f_del=0 group by f_ttname,f_rpid");
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                if (read["f_scount"].ToString() != "0")
                {
                    cc_Train_WorkPlace.Series[1].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_ttname"].ToString() + read["f_name"].ToString() + " " + read["f_rpname"].ToString(), new object[] { ((object)((int)read["f_scount"])) }));
                }
                if (read["f_ecount"].ToString() != "0")
                {
                    cc_Train_WorkPlace.Series[0].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_ttname"].ToString() + read["f_name"].ToString() + " " + read["f_rpname"].ToString(), new object[] { ((object)((int)read["f_ecount"])) }));
                }
                if (read["f_pcount"].ToString() != "0")
                {
                    cc_Train_WorkPlace.Series[2].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_ttname"].ToString() + read["f_name"].ToString() + " " + read["f_rpname"].ToString(), new object[] { ((object)((int)read["f_pcount"])) }));
                }
            }
            read.Close();
            conn.Close();
            conn.Dispose();
        }
        //刷新部门任务统计图
        private void _LoadChartDepWorkPlace()
        {
            string strSQL = "select count(*) as f_count,f_depname,f_state from (select f_state,(select f_name from TS_S_T_P_Department where f_id=(select top 1 f_depid from TS_S_T_P_Stuff where f_id=TS_TR_BU_Work_Main_Relation.f_fid)) as f_depname from TS_TR_BU_Work_Main_Relation where f_rtype in(4,5,6) and f_ftype=0 and f_rid in (select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_bmwid in (select f_id from TS_TR_BU_Work_Main where f_state in (select f_id from TS_S_T_S_RP_DataState where f_state not in (-1,9) and f_del=0) and f_del=0 and f_type=" + _LoadWorkType + ") and f_del=0) and f_del=0) as a group by a.f_depname,a.f_state";
            cc_Dep_WorkPlace.Series[0].Points.Clear();
            cc_Dep_WorkPlace.Series[1].Points.Clear();
            cc_Dep_WorkPlace.Series[2].Points.Clear();
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            //MessageBox.Show(strSQL_TrainCount_Base + strSQL_WorkPlace_BMWID + _strQuery + ") and f_del=0 group by f_ttname,f_rpid");
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                if (read["f_count"].ToString() != "0")
                {
                    if (read["f_state"].ToString() == "0")
                    {
                        cc_Dep_WorkPlace.Series[0].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_depname"].ToString(), new object[] { ((object)((int)read["f_count"])) }));
                    }
                    if (read["f_state"].ToString() == "1")
                    {
                        cc_Dep_WorkPlace.Series[1].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_depname"].ToString(), new object[] { ((object)((int)read["f_count"])) }));
                    }
                    if (read["f_state"].ToString() == "9")
                    {
                        cc_Dep_WorkPlace.Series[2].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_depname"].ToString(), new object[] { ((object)((int)read["f_count"])) }));
                    }
                }
            }
            read.Close();
            conn.Close();
            conn.Dispose();
        }
        #region //--在修机车排程表--//
        private void _LoadTrainInSched()
        {
            string strSQL = "select min(f_pstime) as f_minday,max(f_petime) as f_maxday,datediff(day, min(f_pstime),max(f_petime)) as f_day from TS_TR_BU_Work_Main_Day where f_bmwid in (select f_id from TS_TR_BU_Work_Main where f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) and f_del=0 and f_type=" + _LoadWorkType + ") and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (9)) and f_del=0";
            string[] _MainTime = TS_Win_Class.GetSQlFieldValue(strSQL,new string[] { "f_minday", "f_maxday", "f_day" });
            DateTime _Day = Convert.ToDateTime(_MainTime[0]);
            for (int i = 0; i < Convert.ToInt32(_MainTime[2]) + 1; i++)
            {
                foreach (DevExpress.XtraGrid.Views.BandedGrid.GridBand _cl in gb_f_x01.Children)
                {
                    if (_cl.Name == "gb_f_d" + (i + 1).ToString().PadLeft(2, '0'))
                    {
                        _cl.Children[0].Caption = TS_Win_Class._GetWeekString(_Day.AddDays(i))[0];
                        _cl.Caption = _Day.AddDays(i).Day.ToString();
                    }
                }
            }
            for (int i = Convert.ToInt32(_MainTime[2])+2; i < 32; i++)
            {
                foreach (DevExpress.XtraGrid.Views.BandedGrid.GridBand _cl in gb_f_x01.Children)
                {
                    if (_cl.Name == "gb_f_d" + i.ToString().PadLeft(2, '0'))
                    {
                        _cl.Children[0].Visible = false;
                        _cl.Visible = false;
                    }
                }
            }
            gb_f_x01.Caption = _MainTime[0] + "  至  " + _MainTime[1];
            #region//--计算排程表--//
            string _strTime = " between '" + _MainTime[0] + "' and '" + _MainTime[1] + "'";
            strSQL = "delete TS_TR_TMP_Work_Schedule where f_del=1 and f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId;
            TS_Win_Class.MySqlCom(strSQL);
            strSQL = "select f_id,(select f_name from TS_S_T_P_Train_RP where f_id=TS_TR_BU_Work_Main.f_rpid) as f_rpname,f_rpnum,f_stname,f_type,(select f_name from TS_S_T_P_RailwayStations where f_id=(select f_arsid from TS_S_T_P_Train_List where f_id=TS_TR_BU_Work_Main.f_trainid)) as f_rsname,f_tcname,f_ttname,f_name,f_pitime,f_pstime,f_potime,f_petime,f_pltime from TS_TR_BU_Work_Main where f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) and f_del=0 and f_type=" + _LoadWorkType + " order by f_pitime asc";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            SqlConnection conn1 = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn1.Open();
            SqlCommand comm1 = new SqlCommand("select getdate()", conn1);
            SqlDataReader read1 = comm1.ExecuteReader();
            string _sday = "";
            while (read.Read())
            {
                string _Filed = "f_bmwid,f_rpname,f_rpnum,f_stname,f_type,f_rsname,f_tcname,f_ttname,f_name,f_pitime,f_pstime,f_potime,f_petime,f_pltime";
                string _Value = read["f_id"].ToString() + ",'" + read["f_rpname"].ToString() + "','" + read["f_rpnum"].ToString() + "',' ',0,'" + read["f_rsname"].ToString() + "','" + read["f_tcname"].ToString() + "','" + read["f_ttname"].ToString() + "','" + read["f_ttname"].ToString() + read["f_name"].ToString() +" "+read["f_rpname"].ToString()+ "','" + read["f_pitime"].ToString() + "','" + read["f_pstime"].ToString() + "','" + read["f_potime"].ToString() + "','" + read["f_petime"].ToString() + "','" + read["f_pltime"].ToString() + "'";
                read1.Close();
                strSQL = "select datediff(day,'" + _MainTime[0] + "',f_pstime)as f_dayss,datediff(day,'" + _MainTime[1] + "',f_pstime)as f_dayse,datediff(day,'" + _MainTime[0] + "',f_petime)as f_dayes,datediff(day,'" + _MainTime[1] + "',f_petime)as f_dayee,day(f_pstime) as f_day,(select f_string from TS_S_T_S_ColorList where f_id =(select f_cid from TS_S_T_S_RP_Name_Dictionary where f_id=(select f_ndid from TS_TR_PS_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Day.f_mwdid))) as f_color,(select f_name from TS_S_T_S_RP_Name_Dictionary where f_id=(select f_ndid from TS_TR_PS_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Day.f_mwdid)) as f_ndname,f_name,f_pstime,f_petime,f_length from TS_TR_BU_Work_Main_Day where f_del=0 and f_bmwid=" + read["f_id"].ToString() + "  and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (9)) order by f_order asc";
                //MessageBox.Show(strSQL);
                comm1 = new SqlCommand(strSQL, conn1);
                read1 = comm1.ExecuteReader();
                while (read1.Read())
                {
                    if ((int)read1["f_dayss"] < 0)
                    {
                        for (int n = (int)read1["f_dayss"]; n < (int)read1["f_dayss"] + (int)read1["f_length"]; n++)
                        {
                            if (n >= 0)
                            {
                                _sday = ((DateTime)read1["f_pstime"]).AddDays(n).Day.ToString().PadLeft(2, '0');
                                if (_Filed.IndexOf("f_d" + _sday) > 0)
                                {
                                    //MessageBox.Show("机车号" + read["f_ttname"].ToString() + read["f_name"].ToString() + " 工序 " + read1["f_name"].ToString() + "时间分配出现错误！");
                                }
                                else
                                {
                                    _Filed += ",f_d" + _sday + ",f_c" + _sday;
                                    _Value += ",'" + read1["f_ndname"].ToString() + "','" + read1["f_color"].ToString() + "'";
                                }
                            }
                        }
                    }
                    else if ((int)read1["f_dayee"] > 0)
                    {
                        for (int n = (int)read1["f_dayse"]; n <= 0; n++)
                        {
                            _sday = ((DateTime)read1["f_pstime"]).AddDays(n).Day.ToString().PadLeft(2, '0');
                            if (_Filed.IndexOf("f_d" + _sday) > 0)
                            {
                                //MessageBox.Show("机车号" + read["f_ttname"].ToString() + read["f_name"].ToString() + " 工序 " + read1["f_name"].ToString() + "时间分配出现错误！");
                            }
                            else
                            {
                                _Filed += ",f_d" + _sday + ",f_c" + _sday;
                                _Value += ",'" + read1["f_ndname"].ToString() + "','" + read1["f_color"].ToString() + "'";
                            }
                        }

                    }
                    else
                    {
                        for (int n = (int)read1["f_dayss"]+1; n < (int)read1["f_dayss"]+1+(int)read1["f_length"]; n++)
                        {
                            //_sday = ((DateTime)read1["f_pstime"]).AddDays(n).Day.ToString().PadLeft(2, '0');
                            if (_Filed.IndexOf("f_d" + n.ToString().PadLeft(2, '0')) > 0)
                            {
                                //MessageBox.Show("机车号" + read["f_ttname"].ToString() + read["f_name"].ToString() + " 工序 " + read1["f_name"].ToString() + "时间分配出现错误！");
                            }
                            else
                            {
                                _Filed += ",f_d" + n.ToString().PadLeft(2, '0') + ",f_c" + n.ToString().PadLeft(2, '0');
                                _Value += ",'" + read1["f_ndname"].ToString() + "','" + read1["f_color"].ToString() + "'";
                            }
                        }
                    }
                }
                strSQL = "insert into TS_TR_TMP_Work_Schedule(" + _Filed + ",f_del,f_workerid) values(" + _Value + ",1,"+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
                TS_Win_Class.MySqlCom(strSQL);
                strSQL = "select row_number() over (order by f_id) as f_row,*,CONVERT(varchar(100), f_pitime, 23) as f_pitime,CONVERT(varchar(100), f_potime, 23) as f_potime from TS_TR_TMP_Work_Schedule where f_del=1 and f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId;
                DataTable dt = new DataTable();
                TS_Win_Class.FilledDataTable(strSQL, dt);
                gc_Main.DataSource = dt;
            }
            read1.Close();
            conn1.Close();
            conn1.Dispose();
            read.Close();
            conn.Close();
            conn.Dispose();
            #endregion
        }
        #endregion
        #region //--计划机车排程表--//
        private void _LoadTrainPlaneSched()
        {
            string strSQL = "select min(f_pstime) as f_minday,max(f_petime) as f_maxday,datediff(day, min(f_pstime),max(f_petime)) as f_day from TS_TR_BU_Work_Main_Day where f_bmwid in (select f_id from TS_TR_BU_Work_Main where f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state in (-1)) and f_del=0 and f_type=" + _LoadWorkType + ") and f_del=0";
            string[] _MainTime = TS_Win_Class.GetSQlFieldValue(strSQL, new string[] { "f_minday", "f_maxday", "f_day" });
            DateTime _Day = Convert.ToDateTime(_MainTime[0]);
            for (int i = 0; i < Convert.ToInt32(_MainTime[2]) + 1; i++)
            {
                foreach (DevExpress.XtraGrid.Views.BandedGrid.GridBand _cl in pgb_f_x01.Children)
                {
                    if (_cl.Name == "pgb_f_d" + (i + 1).ToString().PadLeft(2, '0'))
                    {
                        _cl.Children[0].Caption = TS_Win_Class._GetWeekString(_Day.AddDays(i))[0];
                        _cl.Caption = _Day.AddDays(i).Day.ToString();
                    }
                }
            }
            for (int i = Convert.ToInt32(_MainTime[2]) + 2; i < 32; i++)
            {
                foreach (DevExpress.XtraGrid.Views.BandedGrid.GridBand _cl in pgb_f_x01.Children)
                {
                    if (_cl.Name == "pgb_f_d" + i.ToString().PadLeft(2, '0'))
                    {
                        _cl.Children[0].Visible = false;
                        _cl.Visible = false;
                    }
                }
            }
            pgb_f_x01.Caption = _MainTime[0] + "  至  " + _MainTime[1];
            #region//--计算排程表--//
            string _strTime = " between '" + _MainTime[0] + "' and '" + _MainTime[1] + "'";
            strSQL = "delete TS_TR_TMP_Work_Schedule where f_del=2 and f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId;
            TS_Win_Class.MySqlCom(strSQL);
            strSQL = "select f_id,(select f_name from TS_S_T_P_Train_RP where f_id=TS_TR_BU_Work_Main.f_rpid) as f_rpname,f_rpnum,f_stname,f_type,(select f_name from TS_S_T_P_RailwayStations where f_id=(select f_arsid from TS_S_T_P_Train_List where f_id=TS_TR_BU_Work_Main.f_trainid)) as f_rsname,f_tcname,f_ttname,f_name,f_pitime,f_pstime,f_potime,f_petime,f_pltime from TS_TR_BU_Work_Main where f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state in (-1)) and f_del=0 and f_type=" + _LoadWorkType + " order by f_pitime asc";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            SqlConnection conn1 = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn1.Open();
            SqlCommand comm1 = new SqlCommand("select getdate()", conn1);
            SqlDataReader read1 = comm1.ExecuteReader();
            string _sday = "";
            while (read.Read())
            {
                string _Filed = "f_bmwid,f_rpname,f_rpnum,f_stname,f_type,f_rsname,f_tcname,f_ttname,f_name,f_pitime,f_pstime,f_potime,f_petime,f_pltime";
                string _Value = read["f_id"].ToString() + ",'" + read["f_rpname"].ToString() + "','" + read["f_rpnum"].ToString() + "',' ',0,'" + read["f_rsname"].ToString() + "','" + read["f_tcname"].ToString() + "','" + read["f_ttname"].ToString() + "','" + read["f_ttname"].ToString() + read["f_name"].ToString() + " " + read["f_rpname"].ToString() + "','" + read["f_pitime"].ToString() + "','" + read["f_pstime"].ToString() + "','" + read["f_potime"].ToString() + "','" + read["f_petime"].ToString() + "','" + read["f_pltime"].ToString() + "'";
                read1.Close();
                strSQL = "select datediff(day,'" + _MainTime[0] + "',f_pstime)as f_dayss,datediff(day,'" + _MainTime[1] + "',f_pstime)as f_dayse,datediff(day,'" + _MainTime[0] + "',f_petime)as f_dayes,datediff(day,'" + _MainTime[1] + "',f_petime)as f_dayee,day(f_pstime) as f_day,(select f_string from TS_S_T_S_ColorList where f_id =(select f_cid from TS_S_T_S_RP_Name_Dictionary where f_id=(select f_ndid from TS_TR_PS_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Day.f_mwdid))) as f_color,(select f_name from TS_S_T_S_RP_Name_Dictionary where f_id=(select f_ndid from TS_TR_PS_Work_Main_Day where f_id=TS_TR_BU_Work_Main_Day.f_mwdid)) as f_ndname,f_name,f_pstime,f_petime,f_length from TS_TR_BU_Work_Main_Day where f_del=0 and f_bmwid=" + read["f_id"].ToString() + " order by f_order asc";
                //MessageBox.Show(strSQL);
                comm1 = new SqlCommand(strSQL, conn1);
                read1 = comm1.ExecuteReader();
                while (read1.Read())
                {
                    if ((int)read1["f_dayss"] < 0)
                    {
                        for (int n = (int)read1["f_dayss"]; n < (int)read1["f_dayss"] + (int)read1["f_length"]; n++)
                        {
                            if (n >= 0)
                            {
                                _sday = ((DateTime)read1["f_pstime"]).AddDays(n).Day.ToString().PadLeft(2, '0');
                                if (_Filed.IndexOf("f_d" + _sday) > 0)
                                {
                                    //MessageBox.Show("机车号" + read["f_ttname"].ToString() + read["f_name"].ToString() + " 工序 " + read1["f_name"].ToString() + "时间分配出现错误！");
                                }
                                else
                                {
                                    _Filed += ",f_d" + _sday + ",f_c" + _sday;
                                    _Value += ",'" + read1["f_ndname"].ToString() + "','" + read1["f_color"].ToString() + "'";
                                }
                            }
                        }
                    }
                    else if ((int)read1["f_dayee"] > 0)
                    {
                        for (int n = (int)read1["f_dayse"]; n <= 0; n++)
                        {
                            _sday = ((DateTime)read1["f_pstime"]).AddDays(n).Day.ToString().PadLeft(2, '0');
                            if (_Filed.IndexOf("f_d" + _sday) > 0)
                            {
                                //MessageBox.Show("机车号" + read["f_ttname"].ToString() + read["f_name"].ToString() + " 工序 " + read1["f_name"].ToString() + "时间分配出现错误！");
                            }
                            else
                            {
                                _Filed += ",f_d" + _sday + ",f_c" + _sday;
                                _Value += ",'" + read1["f_ndname"].ToString() + "','" + read1["f_color"].ToString() + "'";
                            }
                        }

                    }
                    else
                    {
                        for (int n = (int)read1["f_dayss"] + 1; n < (int)read1["f_dayss"] + 1 + (int)read1["f_length"]; n++)
                        {
                            //_sday = ((DateTime)read1["f_pstime"]).AddDays(n).Day.ToString().PadLeft(2, '0');
                            if (_Filed.IndexOf("f_d" + n.ToString().PadLeft(2, '0')) > 0)
                            {
                                //MessageBox.Show("机车号" + read["f_ttname"].ToString() + read["f_name"].ToString() + " 工序 " + read1["f_name"].ToString() + "时间分配出现错误！");
                            }
                            else
                            {
                                _Filed += ",f_d" + n.ToString().PadLeft(2, '0') + ",f_c" + n.ToString().PadLeft(2, '0');
                                _Value += ",'" + read1["f_ndname"].ToString() + "','" + read1["f_color"].ToString() + "'";
                            }
                        }
                    }
                }
                strSQL = "insert into TS_TR_TMP_Work_Schedule(" + _Filed + ",f_del,f_workerid) values(" + _Value + ",2," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                TS_Win_Class.MySqlCom(strSQL);
                strSQL = "select row_number() over (order by f_id) as f_row,*,CONVERT(varchar(100), f_pitime, 23) as f_pitime,CONVERT(varchar(100), f_potime, 23) as f_potime from TS_TR_TMP_Work_Schedule where f_del=2 and f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId;
                DataTable dt = new DataTable();
                TS_Win_Class.FilledDataTable(strSQL, dt);
                gc_Main_P.DataSource = dt;
            }
            read1.Close();
            conn1.Close();
            conn1.Dispose();
            read.Close();
            conn.Close();
            conn.Dispose();
            #endregion
        }
        #endregion
        #endregion
        #region//--作业数据--//
        #region//--在修机车列表事件--//
        public void InitTrainInList()
        {
            xtt_TrainList.Nodes.Clear();
            ArrayList pList = new ArrayList();
            TrainInRepairTreeList p = new TrainInRepairTreeList();
            //            string strSQL = "select f_id,f_name,(select count(*) from TS_TR_BU_Work_Main where f_del=0 and f_ttid=TS_S_T_P_TrainType.f_id and f_state in (select f_id from TS_TR_B_Work_Main_State where f_del=0 and f_state<>9)) as f_traincount,(select count(*) from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_state in (select f_id from TS_TR_B_Work_Main_Group_WorkPlace_State where f_del=0 and f_state=9) and f_bmwid in (select f_id from TS_TR_BU_Work_Main where f_del=0 and f_ttid=TS_S_T_P_TrainType.f_id and f_state in (select f_id from TS_TR_B_Work_Main_State where f_del=0 and f_state<>9)))*100/(select case count(*) when 0 then 1 else count(*) end from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwid in (select f_id from TS_TR_BU_Work_Main where f_del=0 and f_ttid=TS_S_T_P_TrainType.f_id and f_state in (select f_id from TS_TR_B_Work_Main_State where f_del=0 and f_state<>9))) as f_percent from TS_S_T_P_TrainType where f_del=0 and f_id in (select f_ttid from TS_S_T_P_Train_List where f_del=0 and f_id in (select f_trainid from TS_TR_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_TR_B_Work_Main_State where f_del=0 and f_state<>9)))";
            string strSQL = "select f_id,f_name,(select count(*) from TS_TR_BU_Work_Main where f_del=0 and f_tcid=TS_S_T_P_TrainDepSys.f_id and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9))) as f_traincount from TS_S_T_P_TrainDepSys where f_del=0 and f_id in (select f_tdid from TS_TR_B_Loco_Fittings_Base_List where f_del=0 and f_id in (select f_trainid from TS_TR_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) and f_type=" + _LoadWorkType + "))";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new TrainInRepairTreeList();

                p.f_name = read["f_name"].ToString() + " " + read["f_traincount"].ToString() + "台";
                p.f_id = "T" + read["f_id"].ToString();

                pList.Add(p);
            }
            read.Close();
            strSQL = "select f_id,f_ttname+f_name as f_name,(select f_name from TS_S_T_P_Train_RP where f_id=TS_TR_BU_Work_Main.f_rpid) as f_rpname,f_ttid,f_tcid,(select count(*) from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state in (9)) and f_bmwid=TS_TR_BU_Work_Main.f_id)*100/(select case count(*) when 0 then 1 else count(*) end  from TS_TR_BU_Work_Main_Group_WorkPlace where f_del=0 and f_bmwid=TS_TR_BU_Work_Main.f_id)+TS_TR_BU_Work_Main.f_count as f_percent from TS_TR_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) and f_type=" + _LoadWorkType + " order by f_ttname,f_name asc";
            comm = new SqlCommand(strSQL, conn);
            read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new TrainInRepairTreeList();

                p.f_name = read["f_name"].ToString();
                p.f_id = "L" + read["f_id"].ToString();
                p.f_parent = "T" + read["f_tcid"].ToString();
                p.f_percent = (int)read["f_percent"];
                p.f_rpname = read["f_rpname"].ToString();
                p.f_imgindex = 103;
                pList.Add(p);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            xtt_TrainList.DataSource = pList;
            xtt_TrainList.Nodes.TreeList.ExpandAll();
        }
        private void xtt_TrainList_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                bbi_Train_Delete.Enabled = false;
                bbi_Train_Delete.Tag = "-1";
                bbi_Train_Report_Debug.Enabled = false;
                bbi_Train_Fittings_UpDown_Work.Enabled = false;
                if (xtt_TrainList.FocusedNode["f_id"].ToString().Substring(0, 1) == "L")
                {
                    bbi_Train_Fittings_UpDown_Work.Enabled = true;
                    if (TS_Win_Locomotive_MainForm.pUserInfoMain.GuId == "0")
                    {
                        bbi_Train_Report_Debug.Enabled = true;
                        bbi_Train_Delete.Enabled = true;
                        bbi_Train_Delete.Tag = xtt_TrainList.FocusedNode["f_id"].ToString().Substring(1);
                    }
                }
            }
            catch { }
        }
        #endregion
        #region//--功能按钮--//
        //刷新数据
        private void bbi_Reload_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _ReloadData();
        }
        //手动入检
        private void bbi_Train_Repair_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Locomotive_Repair_Work_ElectricIn_MainForm _uc = new Locomotive_Repair_Work_ElectricIn_MainForm();
            if (_uc.ShowDialog()==DialogResult.OK)
            {
                InitTrainInList();
            }
        }
        //调试报表
        private void bbi_Train_Report_Debug_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserCon.Report.UserCon_Report_Debug_Main _uc = new UserCon.Report.UserCon_Report_Debug_Main();
            _uc._LoadBMWID = xtt_TrainList.FocusedNode["f_id"].ToString().Substring(1);
            _uc._LoadName = xtt_TrainList.FocusedNode["f_name"].ToString();
            _uc._LoadMainID = "10";
            _uc.ShowDialog();
        }
        //删除机车
        private void bbi_Train_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string strSQL = "update TS_TR_BU_Work_Main_Resource set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_del=0 and ((f_maintype=0 and f_mainid=" + bbi_Train_Delete.Tag.ToString() + ") or (f_maintype=1 and f_mainid in(select f_id from TS_TR_BU_Work_Main_Day where f_bmwid=" + bbi_Train_Delete.Tag.ToString() + ")) or (f_maintype=2 and f_mainid in(select f_id from TS_TR_BU_Work_Main_Steps where f_bmwid=" + bbi_Train_Delete.Tag.ToString() + ")) or (f_maintype=3 and f_mainid in(select f_id from TS_TR_BU_Work_Main_Group where f_bmwid=" + bbi_Train_Delete.Tag.ToString() + ")) or (f_maintype=4 and f_mainid in(select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_bmwid=" + bbi_Train_Delete.Tag.ToString() + ")))";
            strSQL += ";update TS_TR_BU_Work_Main_Relation set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_del=0 and ((f_rtype=0 and f_rid=" + bbi_Train_Delete.Tag.ToString() + ") or (f_rtype=1 and f_rid in(select f_id from TS_TR_BU_Work_Main_Day where f_bmwid=" + bbi_Train_Delete.Tag.ToString() + ")) or (f_rtype=2 and f_rid in(select f_id from TS_TR_BU_Work_Main_Steps where f_bmwid=" + bbi_Train_Delete.Tag.ToString() + ")) or (f_rtype=3 and f_rid in(select f_id from TS_TR_BU_Work_Main_Group where f_bmwid=" + bbi_Train_Delete.Tag.ToString() + ")) or (f_rtype in(4,5,6,7,8,9,10) and f_rid in(select f_id from TS_TR_BU_Work_Main_Group_WorkPlace where f_bmwid=" + bbi_Train_Delete.Tag.ToString() + ")))";
            strSQL += ";update TS_TR_BU_Work_Main set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_del=0 and f_id=" + bbi_Train_Delete.Tag.ToString();
            strSQL += ";update TS_TR_BU_Work_Main_Day set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_del=0 and f_bmwid=" + bbi_Train_Delete.Tag.ToString();
            strSQL += ";update TS_TR_BU_Work_Main_Steps set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_del=0 and f_bmwid=" + bbi_Train_Delete.Tag.ToString();
            strSQL += ";update TS_TR_BU_Work_Main_Group set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_del=0 and f_bmwid=" + bbi_Train_Delete.Tag.ToString();
            strSQL += ";update TS_TR_BU_Work_Main_Group_WorkPlace set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_del=0 and f_bmwid=" + bbi_Train_Delete.Tag.ToString();
            strSQL += ";update TS_TR_BU_Work_Main_Group_WorkPlace set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_del=0 and f_bmwid=" + bbi_Train_Delete.Tag.ToString();
            strSQL += ";update TS_S_B_Station_WorkPlace set f_stacknum=f_stacknum-1 where f_id=" + TS_Win_Class.GetSQlFieldValue("select f_tpmapid from TS_TR_BU_Work_Main where f_id=" + bbi_Train_Delete.Tag.ToString(), "f_tpmapid");
            TS_Win_Class.MySqlCom(strSQL);
            _ReloadData();
        }
        //配件上下车作业
        private void bbi_Train_Fittings_UpDown_Work_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TS_XY_Train_Repaire.Locomotive.TrainRepair.Work.Fittings.Locomotive_TrainRepair_Work_Fittings_UpDownTrain_ManaForm _uc = new Work.Fittings.Locomotive_TrainRepair_Work_Fittings_UpDownTrain_ManaForm();
            if (xtt_TrainList.FocusedNode["f_id"].ToString().Substring(0, 1) == "L")
            {
                _uc._LoadTTID = xtt_TrainList.FocusedNode.ParentNode["f_id"].ToString().Substring(1);
                _uc._LoadTTName = xtt_TrainList.FocusedNode.ParentNode["f_name"].ToString();
                _uc._LoadTLID = xtt_TrainList.FocusedNode["f_id"].ToString().Substring(1);
                _uc._LoadTLName = xtt_TrainList.FocusedNode["f_name"].ToString();
                if (_uc.ShowDialog() == DialogResult.OK)
                {
                }
                else
                {
                }
            }
        }
        #endregion

        #endregion

        private void gc_Main_DoubleClick(object sender, EventArgs e)
        {
            if (gv_Main.SelectedRowsCount > 0)
            {
                Locomotive.Plane.Locomotive_Train_Gant_Map _uc = new Locomotive.Plane.Locomotive_Train_Gant_Map();
                _uc._LoadID = gv_Main.GetFocusedRowCellValue("f_bmwid").ToString();
                _uc._LoadName = gv_Main.GetFocusedRowCellValue("f_ttname").ToString() + gv_Main.GetFocusedRowCellValue("f_name").ToString();
                _uc.ShowDialog();
            }
        }

        private void gc_Main_P_DoubleClick(object sender, EventArgs e)
        {
            if (gv_Main_P.SelectedRowsCount > 0)
            {
                Locomotive.Plane.Locomotive_Train_Gant_Map _uc = new Locomotive.Plane.Locomotive_Train_Gant_Map();
                _uc._LoadID = gv_Main_P.GetFocusedRowCellValue("f_bmwid").ToString();
                _uc._LoadName = gv_Main_P.GetFocusedRowCellValue("f_ttname").ToString() + gv_Main_P.GetFocusedRowCellValue("f_name").ToString();
                _uc.ShowDialog();
            }
        }
    }
}